#[derive(Clone, Copy)]
struct RetirementInfo {
    base_retirement_age: i32,
    gap: i32,
    start: i32,
    new_base_retirement_age: i32
}
struct BirthInfo {
    year: i32,
    month: i32,
}
use std::collections::HashMap;

pub fn retire_time(time: &str, tp: &str) -> String {
    // 解析出生年月
    let parts: Vec<&str> = time.split('-').collect();
    let birth_year: i32 = parts[0].parse().expect("Invalid year");
    let birth_month: i32 = parts[1].parse().expect("Invalid month");

    // 创建出生信息结构体
    let birth_info = BirthInfo { year: birth_year, month: birth_month };

    // 根据人员类型确定法定退休年龄、延迟间隔和起始年份
    let retirement_info = get_retirement_info(tp);

    // 计算需要延时退休的月份
    let delay_months = calculate_delay_months(&birth_info, &retirement_info);

    // 计算退休的年份和月份
    let mut retirement_year = birth_year + retirement_info.base_retirement_age + (delay_months / 12);
    let mut retirement_month = birth_month;
    let remain_month = delay_months % 12;
    if birth_month + remain_month > 12 {
        retirement_year += 1;
        retirement_month = (birth_month + remain_month) % 12;
    } else {
        retirement_month += remain_month;
    }

    // 计算退休年纪
    let retirement_age = retirement_info.base_retirement_age as f32 + delay_months as f32 / 12.0;
    // 格式化退休年龄
    let retirement_age_str = if retirement_age.fract() == 0.0 {
        format!("{}", retirement_age as i32)
    } else {
        format!("{:.2}", retirement_age)
    };

    // 格式化输出
    format!("{:04}-{:02},{},{}", retirement_year, retirement_month, retirement_age_str, delay_months)
}

// 获取退休信息
fn get_retirement_info(tp: &str) -> RetirementInfo {
    let retirement_info_map: HashMap<&str, RetirementInfo> = 
    [
        ("原法定退休年龄55周岁女职工", RetirementInfo { base_retirement_age: 55, gap: 4, start: 1970, new_base_retirement_age: 58 }),
        ("原法定退休年龄50周岁女职工", RetirementInfo { base_retirement_age: 50, gap: 2, start: 1975, new_base_retirement_age: 55 }),
        ("男职工", RetirementInfo { base_retirement_age: 60, gap: 4, start: 1965 , new_base_retirement_age: 63}),
    ].iter().cloned().collect();
    *retirement_info_map.get(tp).expect("Unknown type")
}
fn calculate_delay_months(birth_info: &BirthInfo, retirement_info: &RetirementInfo) -> i32 {
    if retirement_info.start > birth_info.year {
        return 0;
    }
    // 计算多个月
    let gap_year = birth_info.year - retirement_info.start;
    let gap_month = birth_info.month - 1;
    let delay_months = gap_year * 12 + gap_month;
    // 计算最多延迟退休时间
    let max_delay_months = (retirement_info.new_base_retirement_age - retirement_info.base_retirement_age) * 12;
    if delay_months / retirement_info.gap + 1 > max_delay_months {
        max_delay_months
    } else {
        delay_months / retirement_info.gap + 1
    }
}
